<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Http\Controllers\Admin\BaseController;

use App\Repositories\OptionRepositoryEloquent;
use App\Models\Option;
use App\Models\Order;
use Illuminate\Http\Request;
use Breadcrumbs, Toastr;

class OrderController extends BaseController{

    protected $option;

    public function __construct(OptionRepositoryEloquent $option)
    {
        parent::__construct();
        $this->option = $option;
    }

    public function index(Request $request){
        $where=[];
        $whereHas=[];
        $data=['name'=>'','option_id'=>0,'mobile'=>'','from'=>'','to'=>''];
        if(!empty($request->all())){
            //if($request->name){
            //dd($request->all());
            $datas = $request->all();
            foreach($datas as $k=>$v){
                if(($k=='name' || $k=='mobile') && $v!=''){
                    $whereHas[] = ["$k",'like','%'.$v.'%'];
                }
                if($k=='from' && $v!=''){
                    $where[] = ['close_time','>',strtotime($v)];
                }
                if($k=='to' && $v!=''){
                    $where[] = ["close_time",'<',strtotime($v)];
                }
                if($k=='option_id' && $v!=0){
                    $where[] = ["$k",'=',$v];
                }
                $data[$k] =$v;
                
            }
            //dd($where);
            //$users = User::where($where)->paginate(20);
        }
        //var_dump($data);
        $options = Option::get();
        $orders = Order::orderBy('id','desc')->whereHas('user',function($query) use($whereHas){
            $query->where($whereHas);
        })->where($where)->paginate(20);
        return view('admin.shop.order.index',compact('orders','data','options'));
    }



}